package jp.sourceforge.akjrcp.subtitle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;

public class TimeSpinner extends Composite{

	private Spinner hourSpinner;
	private Spinner minuteSpinner;
	private Spinner secondSpinner;
	private Spinner millisecondSpinner;

	public TimeSpinner(Composite arg0, int arg1) {
		super(arg0, arg1);
		this.setLayout(new GridLayout(7,false));
		hourSpinner = new Spinner(this,SWT.NULL);
		hourSpinner.setSelection(0);
		hourSpinner.setMaximum(999);
		Label label=new Label(this,SWT.NULL);
		label.setText(":");
		label.setToolTipText("hour");
		minuteSpinner = new Spinner(this,SWT.NULL);
		minuteSpinner.setSelection(0);
		minuteSpinner.setMaximum(60);
		
		Label label2=new Label(this,SWT.NULL);
		label2.setText(":");
		label2.setToolTipText("minute");
		secondSpinner = new Spinner(this,SWT.NULL);
		secondSpinner.setSelection(0);
		secondSpinner.setMaximum(60);
		
		Label label3=new Label(this,SWT.NULL);
		label3.setText(".");
		label3.setToolTipText("second");
		millisecondSpinner = new Spinner(this,SWT.NULL);
		millisecondSpinner.setSelection(0);
		millisecondSpinner.setMaximum(1000);
		
		
		SpinnerModified spinnerModified=new SpinnerModified();
		//hourSpinner.addSelectionListener(spinnerSelection);
		hourSpinner.addModifyListener(spinnerModified);
		minuteSpinner.addModifyListener(spinnerModified);
		secondSpinner.addModifyListener(spinnerModified);
		millisecondSpinner.addModifyListener(spinnerModified);
	}
	public class SpinnerModified implements ModifyListener{

		public void modifyText(ModifyEvent e) {
			Spinner spinner=(Spinner) e.getSource();
			
			if(spinner == hourSpinner){
				
			}else if(spinner == minuteSpinner){
				if(spinner.getSelection()==60){
					spinner.setSelection(0);
					hourSpinner.setSelection(hourSpinner.getSelection()+1);
					
				}
			}else if(spinner == secondSpinner){
				if(spinner.getSelection()==60){
					spinner.setSelection(0);
					minuteSpinner.setSelection(minuteSpinner.getSelection()+1);
					
				}
			}else if(spinner == millisecondSpinner){
				if(spinner.getSelection()==1000){
					spinner.setSelection(0);
					secondSpinner.setSelection(secondSpinner.getSelection()+1);
				}
			}
		}
		
	}

	public void init(){
		hourSpinner.setSelection(0);
		minuteSpinner.setSelection(0);
		secondSpinner.setSelection(0);
		millisecondSpinner.setSelection(0);
	}
	
	public void setTime(long millisecond){
		long hour_millisecond=(60*60*1000);
		int minute_millisecond=60*1000;
		int second_millisecond=1000;
		int hour=(int)(millisecond/hour_millisecond);
		long remain=millisecond%hour_millisecond;
		int minute=(int)(remain/minute_millisecond);
		remain=remain%minute_millisecond;
		int second=(int)(remain/second_millisecond);
		remain=remain%second_millisecond;
		
		hourSpinner.setSelection(hour);
		minuteSpinner.setSelection(minute);
		secondSpinner.setSelection(second);
		millisecondSpinner.setSelection((int)remain);
	}
	public long getTime(){
		long result=millisecondSpinner.getSelection();
		result+=secondSpinner.getSelection()*1000;
		result+=minuteSpinner.getSelection()*60*1000;
		result+=hourSpinner.getSelection()*60*60*1000;
		return result;
	}

}
